const db = require('../../lib/conn_oracle')

exports.getSpecialtiesInfo  = async (req,res)=>{
    // console.log(req.body)
    let {params} = req.body
    let {specialties} = params
    // console.log(specialties)
    let resDatas = []
    let resMsg = ""
    let reqDatas = []
    let specialtiesWhere
    if(specialties && specialties.length > 1){
        specialtiesWhere = `and cust_specnum in (`
        for (let i = 0; i < specialties.length; i++)
        specialtiesWhere += (i > 0)? ", :" + i : ":" + i
        specialtiesWhere += ")"  
        reqDatas.push(...specialties)
    }else if(specialties && specialties.length === 1){
        specialtiesWhere = `and cust_specnum = :specialties`
        reqDatas.push(...specialties)
    }else{specialtiesWhere = ""}
    let sql = `select * from c##wxw.specialty
               where 1 = 1
               ${specialtiesWhere}
               order by cust_specnum
              `    
    try{
        let specialtiesInfo = await db.search(sql,reqDatas)
        resMsg = specialtiesInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = specialtiesInfo.errorNum ? 0 : 1
        resDatas = { specialtiesInfo }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg: "查询失败" + err,
            status: 0
        })
    }
}

exports.editSpecialties = async (req,res)=>{
    // console.log(req.body)
    let {type,data} = req.body
    let {CUST_SPECNUM,SPEC_DESC} = data
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    if(type === "add" || type === "copy"){
        msgType = "新增"
        editDatas = [ CUST_SPECNUM,SPEC_DESC ]
        editSql = `insert into c##wxw.specialty(CUST_SPECNUM,SPEC_DESC)
                   values(:CUST_SPECNUM,:SPEC_DESC)`
        let sql = `select * from c##wxw.specialty where cust_specnum = :CUST_SPECNUM`
        let specialtyInfo = await db.search(sql,[CUST_SPECNUM])
        if(specialtyInfo.length){
            res.json({
                msg:`新增失败，专业${CUST_SPECNUM}已存在`,
                status:0
            })
            return
        }
    }
    if(type === "update"){
        msgType = "更新"
        editDatas = [ SPEC_DESC,CUST_SPECNUM ]
        editSql = `update c##wxw.specialty 
                    set SPEC_DESC=:SPEC_DESC
                    where CUST_SPECNUM=:CUST_SPECNUM`
    }
    if(type === "delete"){
        msgType = "删除"
        editDatas = [ CUST_SPECNUM ]
        editSql = `delete from c##wxw.specialty where CUST_SPECNUM=:CUST_SPECNUM`
    }

    try{
        let newSpecialties = await db.writeTab(editSql,editDatas)
        resMsg = newSpecialties.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newSpecialties.errorNum ? 0 : 1
        resDatas = { newSpecialties }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
    
}

exports.batchEditSpecialties = async (req, res) => {
    // console.log(req.body)
    let { type, data } = req.body
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    let options = {
        autoCommit: true
    }
    if(type === "batchAdd"){
        msgType = "批量新增"
        editDatas = data
        editSql = `insert into c##wxw.specialty(CUST_SPECNUM,SPEC_DESC)
                   values(:CUST_SPECNUM,:SPEC_DESC)`
        let needAddSpecialties = []
        data.forEach(item => {
            item.CUST_SPECNUM && needAddSpecialties.push(item.CUST_SPECNUM)
        });
        // console.log(needAddSpecialties)
        let searchSql = `select p.cust_specnum
                         from c##wxw.specialty p
                         where p.cust_specnum in ('${needAddSpecialties.join("','")}')
                        `
        // console.log(searchSql)
        let specialtyInfo = await db.search2(searchSql)
        // console.log(personGroupInfo)
        let specialtyInfoList = specialtyInfo.map(item => item.CUST_SPECNUM)
        let diff = !specialtyInfoList.length ? [] : needAddSpecialties.filter(item => specialtyInfoList.includes(item))
        // console.log(diff)
        if(diff.length){
            res.json({
                msg:`${msgType}失败，存在重复的专业：${diff.join(",")}`,
                status:0
            })
            return
        }
    }
    if(type === "batchUpdate"){
        msgType = "批量更新"
        editDatas = data
        editSql = `update c##wxw.specialty 
                    set SPEC_DESC=:SPEC_DESC
                    where CUST_SPECNUM=:CUST_SPECNUM`
    }
    if(type === "batchDel"){
        msgType = "批量删除"
        editDatas = data
        editSql = `delete from c##wxw.specialty where CUST_SPECNUM=:CUST_SPECNUM`
    }

    try{
        let newSpecialties = await db.writeManyTab(editSql,editDatas,options)
        resMsg = newSpecialties.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newSpecialties.errorNum ? 0 : 1
        resDatas = { newSpecialties }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }

}